{extend name="public/base"/}
{block name="style"}
{/block}
{block name="content"}
    <div class="data-list" data-url="{:url('index')}">
        <form class="layui-form inline-form">
            <div class="pull-left">
                <div class="layui-inline">
                    <input class="layui-input input-order-sn" autofocus autocomplete="off" name="pick_sn" id="pick-sn" placeholder="拣货单号">
                </div>
                <div class="layui-inline">
                    <input class="layui-input laydate-range" autocomplete="off" name="print_time" placeholder="打印时间">
                </div>
                <div class="layui-inline">
                    <select name="is_print" lay-search lay-filter="data-list">
                        <option value="">是否打印</option>
                        {foreach $is_print as $k=>$v}
                        <option value="{$k}">{$v}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <select name="is_end" lay-search lay-filter="data-list">
                        <option value="">是否完成</option>
                        {foreach $is_end as $k=>$v}
                        <option value="{$k}">{$v}</option>
                        {/foreach}
                    </select>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-sm layui-btn-normal search"><i class="layui-icon layui-icon-search"></i></button>
                </div>
            </div>
        </form>
        <div class="layui-row">
            <div class="layui-inline layui-btn-group">
                {if $is_show_generate}
                <a id="generate-pick" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon layui-icon-cart"></i>生成拣货单</a>
                {/if}
            </div>
        </div>
        <div class="data">
            <p><i class="fa fa-spinner fa-spin"></i> 加载中...</p>
        </div>
    </div>
{/block}
{block name="script"}
<link rel="stylesheet" href="__PUBLIC__/css/print.min.css">
<script type="text/javascript" src="__PUBLIC__/js/print.min.js"></script>
<script type="text/javascript">
    layui.use(['layer', 'jquery', 'longli','printPDF'], function()
    {
        var layer = layui.layer
            , $ = layui.jquery;
        // 扫描拣货单
        $("#pick-sn").keydown(function(event)
        {
            if(event.keyCode != 13) return;
            var _that = $(this)
                , sn = _that.val().trim()
                , item = $(".data table ." + sn).attr("item");
            if(item == 1)
            {
                setTimeout(()=>{_that.val('');}, 200);
                return;
            }
            location.href = "{:url('order/pick/info')}?pick_sn=" + sn;
        });
        // 打印拣货单
        $(document).on("click", ".print-pick", function()
        {
            var $this = $(this)
                , pickId = $this.attr("item");
            if(!pickId) return false;
            layer.load(0, {shade: false});
            $.get("{:url('print_pick')}", {"pick_id": pickId}).done(function(response)
            {
                layer.closeAll('loading');
                var time = 1500;
                if(response.code == 1)
                {
                    $(".data").closest('.data-list').getList();
                    printJS({printable: response.data.file, type: 'pdf', base64: true, showModal:true});
                }
                else
                {
                    layer.msg(response.msg, {icon: 5, time: time});
                }
            }).error(function(xhr)
            {
                layer.msg('服务器异常，请稍后重试~', {icon: 5});
                layer.closeAll('loading');
            });
        });
        // 生成拣货单
        $("#generate-pick").click(function()
        {
            var $this = $(this);
            if($this.data("is-click")) return false;
            $this.data("is-click", true);
            layer.load(0, {shade: false});
            $.get("{:url('generate')}").done(function(response)
            {
                $this.data("is-click", false);
                layer.closeAll('loading');
                var icon = 5, time = 1500;
                if(response.code == 1)
                {
                    $(".data").closest('.data-list').getList();
                    icon = 6;
                }
                layer.msg(response.msg, {icon: icon, time: time});
            }).error(function(xhr)
            {
                layer.msg('服务器异常，请稍后重试~', {icon: 5});
                layer.closeAll('loading');
            });
        });

        // 重围打印状态
        $(document).on("click", "#reset-print", function()
        {
            var ids = getIds(".info-id");
            if(ids.length < 1) return;
            $.get("{:url('reset')}", {ids: ids.join(","), "pick_id": $("#pick-id").val()}).done(function(response)
            {
                var icon = 5, time = 1500;
                if(response.code == 1)
                {
                    icon = 6;
                    $(".data").closest('.data-list').getList();
                    layer.closeAll();
                }
                layer.msg(response.msg, {icon, time});
            });
        });

        // 鼠标点击打印
        var isClickPrint = false;
        $(document).on("click", ".print-label", function()
        {
            var $this = $(this)
                , isPrint = $this.attr("is-print") * 1
                , infoId = $this.attr("info-id");
            if(!infoId || isPrint > 0 || isClickPrint) return false;
            isClickPrint = true;
            layer.load(0, {shade: false});
            $.get("{:url('print_label')}", {"info_id": infoId}).done(function(response)
            {
                isClickPrint = false;
                layer.closeAll('loading');
                var time = 1500;
                if(response.code != 1)
                {
                    layer.msg(response.msg, {icon: 5, time: time});
                    return false;
                }
                $this.parent().parent()
                    .find(".print-status")
                    .removeClass("text-muted")
                    .addClass("text-yellow")
                    .find(".item-text")
                    .text("是");
                $this.attr("is-print", response.data.user_id).hide();
                // 执行打印
                layui.printPDF.doPrint(DOMAIN_URL + response.data.label_url, response.data.size);
                // 如果已完成拣货单
                if(response.data.is_end)
                {
                    if(response.data.type) layui.longli.playMP3(response.data.type);
                    $(".data").closest('.data-list').getList();
                    layer.closeAll();
                    layer.msg(response.msg, {icon: 6, time: time});
                }
            }).error(function(xhr)
            {
                layer.msg('服务器异常，请稍后重试~', {icon: 5});
                layer.closeAll('loading');
            });
        });
    });
</script>
{/block}